import { mapActions, mapMutations } from 'vuex';
import { Message } from 'element-ui';
const methods = {
  ...mapMutations({
    setPage: 'schedule/setPage',
    setWhichWeek: 'schedule/setWhichWeek',
    setEditTarget: 'schedule/setEditTarget',
    setDepartment: 'schedule/setDepartment',
    setName: 'schedule/setName',
    setNewDutyToken: 'schedule/setNewDutyToken'
  }),
  ...mapActions({
    requestServiceTypeOptions: 'schedule/requestServiceTypeOptions',
    requestSchedules: 'schedule/requestSchedules',
    requestDepartments: 'schedule/requestDepartments',
    updatePersonDuty: 'schedule/updatePersonDuty',
    deletePersonDuty: 'schedule/deletePersonDuty',
    addPersonDuty: 'schedule/addPersonDuty'
  }),

  // 触发重新请求表格数据的函数
  async interceptRequestSchedules() {
    this.tableDataLoading = true;
    const res = await this.requestSchedules();
    if (res === true) {
      this.tableDataLoading = false;
    }
  },
  handleChoosePage(choosedPage) {
    this.setPage(choosedPage);
    this.interceptRequestSchedules();
  },
  handleChooseWeek(e) {
    if (e === null) {
      return;
    }
    const whichWeek = e.getTime();
    this.setWhichWeek(whichWeek);
    this.interceptRequestSchedules();
  },

  async handleSubmitAdd(duty) {
    const res = await this.addPersonDuty(duty);
    if (res === true) {
      Message({
        message: '新增任务成功！',
        type: 'success'
      });
      this.$refs.add_dialog.ifDialog = false;
    }
  },

  // 编辑
  handleEditDuty(editTarget) {
    this.setEditTarget(editTarget);
    this.$refs.edit_dialog.ifDialog = true;
  },
  async handleConfirmEditDuty(duty) {
    const res = await this.updatePersonDuty(duty);
    if (res === true) {
      this.$refs.edit_dialog.ifDialog = false;
      Message({
        message: '修改任务成功！',
        type: 'success'
      });
      this.interceptRequestSchedules();
    }
  },

  async handleDeleteDuty(duty) {
    const res = await this.deletePersonDuty(duty);
    if (res === true) {
      this.$refs.edit_dialog.ifDialog = false;
      Message({
        message: '删除任务成功！',
        type: 'success'
      });
      this.interceptRequestSchedules();
    }
  },

  handleUpdateSearchCondition({ department, name }) {
    this.setDepartment(department);
    this.setName(name);
  },
  handleSearch() {
    this.interceptRequestSchedules();
  },
  handleAddDuty({ row, day }) {
    const add_dialog = this.$refs.add_dialog;
    this.setNewDutyToken({
      name: row.name,
      day
    });
    add_dialog.ifDialog = true;
  }
};
export default methods;
